// 将源数组裁成固定列，每列固定行
export function sliceList(src, column, columnCount) {
    let result = [];
    if (!src instanceof Array) {
        return result;
    }
    let length = src.length;
    for (var i = 0; i < column; i++) {
        let startPosition = i * columnCount;
        let endPosition = (i + 1) * columnCount;
        if (endPosition < length) {
            result.push(src.slice(startPosition, endPosition));
        } else {
            if (startPosition < length) {
                result.push(src.slice(startPosition, length));
            } else {
                result.push([])
            }
        }
    }
    return result;
}

// 把数组按指定步长分组，返回新数组
export function stepArr(arr, step) {
    let result = [];
    if (!(arr instanceof Array)) {
        return result;
    }
    let len = arr.length;
    let line = Math.ceil(len / step);
    for (var i = 0; i < line; i++) {
        result.push(arr.slice(i * step, i * step + step))
    }

    return result;
}

// 计算当前用时
export function getUseTime(time) {
    let oldTime = (new Date(time)).getTime();
    let currentTime = new Date().getTime();
    return (currentTime - oldTime) / 1000;
}

// 返回时间间隔
export function getTimeInterval(startTime, endTime) {
    let start = (new Date(startTime)).getTime();
    let end = (new Date(endTime)).getTime();
    return getTimeStr((start - end) / 1000);
}


// 时间差转时分秒格式
export function getTimeDiffer(startTime, endTime) {
    let start = + new Date(startTime);
    let end = + new Date(endTime);
    let reduce = parseInt((end - start) / 1000);
    let h = parseInt(reduce / 3600);
    reduce -= h * 3600;
    let m = parseInt(reduce / 60);
    reduce -= m * 60;
    let s = reduce;
    h = h < 10 ? "0" + h : h;
    m = m < 10 ? "0" + m : m;
    s = s < 10 ? "0" + s : s;
    return h + ':' + m + ':' + s;
}

// 将当前用时转换成时分秒
export function getTimeStr(timeData) {
    let h = Math.floor(timeData / 3600);
    let minute = timeData % 3600;
    let m = Math.floor(minute / 60);
    let s = Math.floor(timeData % 60);
    var time = "";
    if (h > 0) {
        if (h < 10) {
            time += "0";
        }
        time += h;
        time += ":";
    }
    if (m < 10) {
        time += "0";
    }
    time += m;
    time += ":";
    if (s < 10) {
        time += "0";
    }
    time += s;
    return time;
}


// 判断对象是否为空
export function isEmptyObject(obj) {
    for (let key in obj) return false
    return true
}

// 转为字符串
export function getString(obj) {
    for (let k in obj) {
        if (typeof obj[k] === 'object') {
            getString(obj[k])
        } else {
            obj[k] = obj[k].toString()
        }
    }
    return obj
}

// 判断是否为对象
export function isObject(obj) {
    return Object.prototype.toString.call(obj) === '[object Object]'
}

// 判断是否为數組
export function isArray(arr) {
    if (typeof Array.isArray === 'function') {
        return Array.isArray(arr)
    } else {
        return Object.prototype.toString.call(arr) === '[object Array]'
    }
}

// 判断是否为FormData
export function isFormData(v) {
    return Object.prototype.toString.call(v) === '[object FormData]'
}

// 筛选对象内空的元素 filter Object Value
export function filterObjEmptyValue(obj) {
    let param = {}
    if (obj === null || obj === undefined || obj === '') return param
    for (let key in obj) {
        if (typeof obj[key] === 'object' && !isEmptyObject(obj[key])) {
            param[key] = filterObjEmptyValue(obj[key])
        } else if (obj[key] !== null && obj[key] !== undefined && obj[key] !== '' && !isEmptyObject(obj[key])) {
            param[key] = obj[key]
        }
    }
    return param
}

// 时间戳 转 时间格式
export function formatTime(v) {
    if (!v) return v
    const date = new Date(v)
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
    return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

// 时间戳 转 时间格式
export function formatTime2(v) {
    if (!v) return v
    const date = new Date(v)
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    return [year, month, day].map(formatNumber).join('-')
}

// 1位数前面补0
export function formatNumber(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
}

// 获取URL参数
export function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return decodeURI(r[2]);
    return null;
}

export function formatDate(date, fmt) {
    date = date || new Date()
    fmt = fmt || 'yyyy-MM-dd HH:mm:ss'
    let o = {
        'M+': date.getMonth() + 1,                   // 月份
        'd+': date.getDate(),                        // 日
        'h+': date.getHours(),                       // 小时
        'm+': date.getMinutes(),                     // 分
        's+': date.getSeconds(),                     // 秒
        'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
        'S': date.getMilliseconds()                 // 毫秒
    }

    if (/(y+)/i.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }

    for (var k in o) {
        if (new RegExp('(' + k + ')').test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
        }
    }
    return fmt
}

